﻿using System.ComponentModel;
using System.Windows.Forms;
using System.IO;
using System;

namespace ToolGab.Controls
{
    public partial class TreeViewFolder : TreeView
    {
        public TreeViewFolder()
        {
            InitializeComponent();
            LoadDriveInTreeView();
        }

        public TreeViewFolder(IContainer container)
        {
            container.Add(this);

            InitializeComponent();
            LoadDriveInTreeView();
        }


        ///----------------------------------------------------------------------------------------
        /// <summary>
        /// Load all the fixed,removable or a network drive.
        /// </summary>
        ///----------------------------------------------------------------------------------------
        private void LoadDriveInTreeView()
        {
            this.Nodes.Clear();
            foreach (DriveInfo di in DriveInfo.GetDrives())
            {
                // Make sure that the drive is ready
                if (di.IsReady && (
                    di.DriveType == DriveType.Fixed ||
                    di.DriveType == DriveType.Network ||
                    di.DriveType == DriveType.Removable
                    ))
                {
                    // Add a new node
                    TreeNode tn = this.Nodes.Add(di.VolumeLabel + " (" + di.Name + ")");
                    tn.Tag = di.RootDirectory;

                    // For all directories in the root folder, we add a new node in
                    // the TreeView
                    foreach (DirectoryInfo dir in di.RootDirectory.GetDirectories())
                    {
                        TreeNode tnDir = tn.Nodes.Add(dir.Name, dir.Name);
                        tnDir.Tag = dir;
                    }
                }
            }
        }
        ///----------------------------------------------------------------------------------------
        /// <summary>
        /// Load all the subfolders of the nodes.
        /// </summary>
        ///----------------------------------------------------------------------------------------
        private void tvFolder_BeforeExpand(object sender, TreeViewCancelEventArgs e)
        {
            // For all the nodes in the nodes that is about to be expand
            foreach (TreeNode node in e.Node.Nodes)
            {
                // Make sure that the tag is a directory info
                if (node.Tag is DirectoryInfo)
                {
                    // Get the DirectoryInfo of the node by the Tag
                    DirectoryInfo di = (DirectoryInfo)node.Tag;

                    // We have to use a Try, because some folders are not accessible and can cause
                    // System error
                    try
                    {
                        // Get the list of directories, this is where it can crash
                        DirectoryInfo[] diList = di.GetDirectories();

                        // For each directory
                        for (int i = 0; i < diList.Length; i++)
                        {
                            // If the directory is already there, we don't add it
                            if (!node.Nodes.ContainsKey(diList[i].Name))
                            {
                                TreeNode tnDir = node.Nodes.Add(diList[i].Name, diList[i].Name);
                                tnDir.Tag = diList[i];
                            }

                        }
                    }
                    catch (Exception)
                    {

                    }


                }
            }
        }
    }

}
